<!--
 * @Author: cqg 
 * @Date: 2020-12-08 14:02:19
 * @LastEditors: cqg
 * @LastEditTime: 2020-12-31 15:14:49
 * @Description: file content
-->
<template>
  <basic-container>
    <dataList
      :pageInfo="pager"
      :filterList="filterList"
      :listData="tableData"
      :selectData="selectDataArr"
      placeholder="搜索工程名称"
      searchName="projectName"
      @menu-click="handleMenuClick"
      openSelection
    >
      <el-table ref="multipleTable" :data="tableData" @selection-change="$event => (selectDataArr = $event)">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="工程名称" width="140" align="center" prop="projectName" />
        <el-table-column label="合同编号" width="160" align="center" prop="contractNum" />
        <el-table-column label="合同类型 " width="140" prop="contractType" :formatter="dataFormatter" />
        <el-table-column label="乙方单位名称" align="center" prop="partybUnit" />
        <el-table-column label="验收日期" align="center" prop="acceptDate" />
        <el-table-column label="验收结果" align="center" prop="acceptResult" :formatter="dataFormatter" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" v-show=" ['600','610'].includes(scope.row.projectStatus)" @click="handleMenuClick('edit', scope.row)">编辑</el-button>
            <span v-show=" ['600','610'].includes(scope.row.projectStatus)">&nbsp;|&nbsp;</span>
            <el-button @click="handleMenuClick('preview', scope.row)" type="text" size="small">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
    </dataList>
    <diaLog ref="dialog" />
  </basic-container>
</template>

<script>
import { query, remove } from "./api.js";
import diaLog from "./dialog.vue";
import { getDict } from "@/utils";

export default {
  provide () {
    return {
      initList: this.queryData
    };
  },
  components: {
    diaLog
  },

  data () {
    return {
      // 表格列表数据
      tableData: [],
      // 选中的数据
      selectDataArr: [],
      // 请求参数
      params: {
        current: 1,
        size: 10,
        projectName: ""
      },
      // 分页
      pager: {
        total: 0, // 总页数
        current: 1, // 当前页数
        size: 10 // 每页显示多少条
      },
      //筛选项
      filterList: [
        {
          label: "验收日期",
          inputType: "daterange",
          name: ["acceptDateStart", "acceptDateEnd"]
        },
        {
          label: "验收结果",
          inputType: "radio",
          name: "acceptResult",
          children: getDict("epc_accept_result")
        },
        {
          label: "工程类型",
          inputType: "dict",
          dictName: "epc_project_type",
          name: "projectType",
          multiple: true
        },
        {
          label: "合同类型",
          inputType: "dict",
          dictName: "epc_contract_type",
          name: "contractType",
          multiple: true
        }
      ]
    };
  },
  beforeMount () {
    this.queryData();
  },
  methods: {
    // 数据请求
    queryData (params = {}) {
      let req = { ...this.params, ...params };
      query(req).then(res => {
        const { current, size, total, records } = res.data.data;
        this.tableData = records;
        this.params = req;
        this.pager = { total, current, size };
      });
    },
    // 菜单点击监听
    handleMenuClick (type, row) {
      switch (type) {
        case "add":
        case "edit":
        case "preview":
          this.$refs.dialog.show(type, row);
          break;
        case "delete":
          this.remove();
          break;
      }
    },
    // 删除
    remove () {
      let newArray = []
      let ids = this.selectDataArr.map((item, i) => {
        !['600','610'].includes(item.projectStatus) && newArray.push(`${item.projectName}`)
        return item.projectAcceptId
      });
      if (newArray.length)
        this.$message.warning(`工程【${newArray}】不可删除`);
      else {
        this.$confirm('此操作将永久删除登记数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async() => {
          const rps = await remove(ids);
          rps.data.code == 0 && this.$message.success("删除成功");
          this.queryData();
        }).catch(() => {
          this.$message.info('已取消删除');
        });

      }
    },
    //格式化表格字典值
    dataFormatter (row, column, callValue) {
      let type = "";
      switch (column.property) {
        //合同类型
        case "contractType":
          type = getDict("epc_contract_type").find(
            item => item.value == callValue
          );
          return type ? type.label : "";
        //验收结果
        case "acceptResult":
          type = getDict("epc_accept_result").find(
            item => item.value == callValue
          );
          return type ? type.label : "";
      }
    }
  }
};
</script>
